<?php

namespace Controller;

/**
 * msg 消息处理
 *
 * @author dongasai
 */
class Msg
{

    /**
     * 基本消息处理
     * @param type $client_id
     * @param type $msgData
     */
    public static function run($client_id, $msgData)
    {

    }

    /**
     * 发送给用户的消息处理
     * @param type $client_id
     * @param type $msgData
     */
    public static function toUser($client_id, $msgData)
    {
        $msg = \Service\Msg::toUser($client_id, $msgData);
        sendToCurrentClient($msg);
    }

    /**
     * 发送给群组 的消息处理
     * @param type $client_id
     * @param type $msgData
     */
    public static function group($client_id, $msgData)
    {
        //验证群组是否可以接受消息
        //发送给群组
    }

    /**
     * 发送消息
     * @param unknown $client_id
     * @param unknown $message_data
     * @throws \Exception
     * @return \GatewayWorker\Lib\boolean|void
     */
    protected static function say($client_id, $message_data)
    {
        echo "runing say 111";
        // 非法请求
        if (!isset($_SESSION['room_id'])) {
            Gateway::sendToClient($client_id, '{"type":"error","id":"1"}');
            return;
        }

        $room_id     = $_SESSION['room_id'];
        $client_name = $_SESSION['client_name'];
        // 私聊
        // 客户端发言 message: {type:say, to_client_id:xx, content:xx}
        if ($message_data['to_client_id'] != 'all') {
            $new_message            = array(
                'type'                => 'say',
                'from_client_id'      => $client_id,
                'from_client_name'    => $client_name,
                'from_client_headimg' => $message_data['from_headimg'],
                'from_client_uid'     => $message_data['from_uid'],
                'to_client_id'        => $message_data['to_client_id'],
                'content'             => "" . nl2br(htmlspecialchars($message_data['content'])),
                'time'                => date('Y-m-d H:i:s'),
            );
            Gateway::sendToClient($message_data['to_client_id'], json_encode($new_message));
            $new_message['content'] = "" . nl2br(htmlspecialchars($message_data['content']));
            return Gateway::sendToCurrentClient(json_encode($new_message));
        }

        // 向大家说
        $room_clients_list = self::getClientListFromRoom($_SESSION['room_id']);
        $client_id_array   = array_keys($room_clients_list);
        $new_message       = array(
            'type'                => 'say',
            'from_client_id'      => $client_id,
            'from_client_name'    => $client_name,
            'from_client_headimg' => $message_data['from_headimg'],
            'from_client_uid'     => $message_data['from_uid'],
            'to_client_id'        => 'all',
            'content'             => nl2br(htmlspecialchars($message_data['content'])),
            'time'                => date('Y-m-d H:i:s'),
        );
        self::addmesforroom($room_id, $new_message);
        return Gateway::sendToAll(json_encode($new_message), $client_id_array);
    }

    /**
     * 增加消息记录
     * @param unknown $room_id
     * @param unknown $mes
     */
    protected static function addmesforroom($room_id, $mes)
    {
        echo "runing addmesforroom for  $room_id \n";
        $key = "Room_mes_list_" . $room_id;

        $Room_mes_list = PSD\Cache::getval($key);

        if (is_array($Room_mes_list)) {

        } else {
            $Room_mes_list = array();
        }
        $Room_mes_list[] = $mes;
        if (count($Room_mes_list) > 11) {
            array_shift($Room_mes_list);
        }

//        var_dump($Room_mes_list);

        return $Room_mes_list = PSD\Cache::setval($key, $Room_mes_list, 2592000);
    }

    /**
     * 发送消息 给UID
     *
     */
    static function sendToUid($uid, $new_message)
    {
        echo "runing event sendToUid ...... \n";
        $key = "User_id_" . $uid;
        $cid = PSD\Cache::getval($key);

        return Gateway::sendToClient($cid, json_encode($new_message));
    }

    /**
     *
     * @param unknown $client_id
     * @param unknown $message_data
     */
    static function saytouid($client_id, $message_data)
    {
        echo "runing saytouid 111";
        // 非法请求
        var_dump($message_data);

        if (!isset($_SESSION['room_id'])) {
            Gateway::sendToClient($client_id, '{"type":"error","id":"1"}');
            return;
        }

        $room_id     = $_SESSION['room_id'];
        $client_name = $_SESSION['client_name'];
        $new_message = array(
            'type'                => 'say',
            'from_client_id'      => $client_id,
            'from_client_name'    => $client_name,
            'from_client_headimg' => $message_data['from_headimg'],
            'from_client_uid'     => $message_data['from_uid'],
            'to_client_id'        => $message_data['to_client_id'],
            'content'             => "" . nl2br(htmlspecialchars($message_data['content'])),
            'time'                => date('Y-m-d H:i:s'),
        );
        if (!self::sendToUid($message_data['to_uid'], $new_message)) {
            //该会员不在线
            $mes1 = array(
                'type'    => 'return',
                'content' => 'online_no'
            );
            Gateway::sendToClient($client_id, json_encode($mes1));
        } else {
            $new_message['content'] = "" . nl2br(htmlspecialchars($message_data['content']));
            return Gateway::sendToCurrentClient(json_encode($new_message));
        }
    }

}
